let app = getApp()
Page({
  data: {
    store_voucher: true,
    platform_voucher: false,
    store_voucher_list: '',
    platform_voucher_list: '',
    is_show_time: 1,
    request_num: 0, //加载底部使用
    k: '',
    u: '',
    page: 1,
    firstRow: 0
  },
  onLoad: function(options) {
    new app.ToastPannel();
    var that = this
    //var fromto = options.fromto
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    if (uid) {

      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Voucher&met=vList&typ=json',
        method: 'GET',
        data: {
          k: key,
          u: uid,
          rows: 10,
          page: 1,
          firstRow: 0
        },
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {

          that.setData({
            store_voucher_list: res.data.data.voucher.items,
            is_show_time: 0,
            k: key,
            u: uid,
            store_voucher: true,
            platform_voucher: false,
            page: 1,
            firstRow: 0,
            request_num: 0
          })
        }
      })
    }
  },
  //点击店铺代金劵
  store_voucher: function() {
    this.onLoad()
  },
  //点击平台红包
  platform_voucher: function() {
    var that = this
    console.log(that);
    if (that.data.u) {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=RedPacket&met=redPacket&typ=json&ua=wap&orderby=default',
        method: 'GET',
        data: {
          k: that.data.k,
          u: that.data.u
        },
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {

          that.setData({
            platform_voucher_list: res.data.data.redpacket.items,
            is_show_time: 0,
            store_voucher: false,
            platform_voucher: true,
            page: 1,
            firstRow: 0,
            request_num: 0
          })
        }
      })
    }
  },
  //立即领取
  now_receive: function(e) {
    var that = this
    var vid = e.currentTarget.dataset.vid
    var keys = e.currentTarget.dataset.keys
    var store_voucher_list = that.data.store_voucher_list
    if (vid) {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Voucher&met=getVoucherById&typ=json',
        method: 'GET',
        data: {
          vid: vid
        },
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          var voucher_t_eachlimit = res.data.data.voucher_t_eachlimit
          if (voucher_t_eachlimit > 0) {
            var content = '每个ID限领' + voucher_t_eachlimit + '张';
          } else {
            var content = '每个ID无限制';
          }
          wx.showModal({
            title: '',
            content: content,
            success: function(res) {
              if (res.confirm) {
                wx.request({
                  url: app.configData.shop.api_url + '/index.php?ctl=Voucher&met=receiveVoucher&typ=json',
                  method: 'GET',
                  data: {
                    vid: vid,
                    k: that.data.k,
                    u: that.data.u
                  },
                  header: {
                    'content-type': 'application/json'
                  },
                  success: function(res) {
                    if (res.data.status == 200) {
                      that.show(res.data.msg)
                      store_voucher_list[keys].voucher_t_giveout = Number(store_voucher_list[keys].voucher_t_giveout) + 1
                      that.setData({
                        store_voucher_list: store_voucher_list
                      })
                    } else {
                      that.show(res.data.msg)
                    }
                  }
                })
              }
            }
          })
        }
      })
    }
  },
  //立即领取平台红包
  now_platform: function(e) {
    var that = this
    var platform_voucher_list = that.data.platform_voucher_list
    var red_packet_t_id = e.currentTarget.dataset.redpacket_t_id
    var keyss = e.currentTarget.dataset.keyss
    if (red_packet_t_id) {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=RedPacket&met=receiveRedPacket&typ=json',
        method: 'GET',
        data: {
          k: that.data.k,
          u: that.data.u,
          red_packet_t_id: red_packet_t_id
        },
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          if (res.data.status == 200) {
            that.show(res.data.msg)
            platform_voucher_list[keyss].redpacket_t_giveout = Number(platform_voucher_list[keyss].redpacket_t_giveout) + 1
            that.setData({
              platform_voucher_list: platform_voucher_list
            })

          } else {
            that.show(res.data.msg)
          }
        }
      })
    }

  },

  //页面上拉触底事件的处理函数
  onReachBottom: function() {
    var that = this
    if (that.data.request_num == 1) {
      return false
    }
    that.setData({
      page: that.data.page + 1,
      firstRow: that.data.firstRow + 10
    })
    if (that.data.store_voucher) {
      var url = app.configData.shop.api_url + '/index.php?ctl=Voucher&met=vList&typ=json'
      var data = {
        k: that.data.k,
        u: that.data.u,
        rows: 10,
        page: that.data.page
      }
    } else {
      var url = app.configData.shop.api_url + '/index.php?ctl=RedPacket&met=redPacket&typ=json&ua=wap&orderby=default'
      var data = {
        k: that.data.k,
        u: that.data.u,
        firstRow: that.data.firstRow
      }
    }
    wx.request({
      url: url,
      header: {
        'content-type': 'application/json'
      },
      data: data,
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (that.data.store_voucher) {
          var store_voucher_list = that.data.store_voucher_list
          if (res.data.data.voucher.items.length > 0) {

            for (var z in res.data.data.voucher.items) {
              store_voucher_list.push(res.data.data.voucher.items[z])
            }
            that.setData({
              store_voucher_list: store_voucher_list,
              is_show_time: 0,
              request_num: 0

            })
          } else {
            that.setData({
              request_num: 1,
              is_show_time: 0

            })
          }

        } else {

          var platform_voucher_list = that.data.platform_voucher_list
          if (res.data.data.redpacket.items.length > 0) {

            for (var z in res.data.data.redpacket.items) {
              platform_voucher_list.push(res.data.data.redpacket.items[z])
            }
            that.setData({
              platform_voucher_list: platform_voucher_list,
              is_show_time: 0,
              request_num: 0

            })
          } else {
            that.setData({
              request_num: 1,
              is_show_time: 0

            })
          }

        }

      }
    })
  },
  bindhref: function() {
    app.goTop()
    this.setData({
      show: 0
    })
  },
  //一键到顶
  onPageScroll: function(e) {
    // 获取滚动条当前位置
    var that = this
    wx.getSystemInfo({
      success: function(res) {

        if (e.scrollTop + res.windowHeight > res.windowHeight) {

          that.setData({
            show: 1
          })
        } else {

          that.setData({

            show: 0
          })
        }
      },
    })
  },

})